package com.cheng.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
//循环发送
public class UDPSenderDemon01 {
    public static void main(String[] args) throws Exception {
        //建立Socket连接
        DatagramSocket socket = new DatagramSocket(8888);
        //准备要发送的数据
        // 用System.in从控制台得到数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while(true){
            //把从数据台得到的消息读取出来
            String data = reader.readLine();
            byte[] dataBytes = data.getBytes();
            //                                        要发送的数据       数据的长度             接收数据的地址
            DatagramPacket packet = new DatagramPacket(dataBytes,0,dataBytes.length,new InetSocketAddress("localhost", 6666));

            //用DatagramSocket发送数据包
            socket.send(packet);
            byte[] data1 = packet.getData();
            String sendData = new String(data1,0,data1.length);
            if(dataBytes.equals("再见")){
                break;
            }
        }


        //关闭连接
        socket.close();


    }
}
